package com.phb.puhuibao.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.idp.pub.entity.Pager;
import com.idp.pub.service.IBaseService;
import com.idp.pub.web.controller.BaseController;
import com.phb.puhuibao.entity.UserMessage;
import com.phb.puhuibao.service.MobileUserService;
import com.phb.puhuibao.service.UserMessageService;

@Controller
@RequestMapping(value = "/userMessage")
public class UserMessageController extends BaseController<UserMessage, String> {
	@Resource(name = "userMessageService")
	public void setBaseService(IBaseService<UserMessage, String> baseService) {
		super.setBaseService(baseService);
	}
	
	@javax.annotation.Resource(name = "jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	@Resource(name = "userMessageService")
	private UserMessageService userMessageService;
	//-----消息首页-----
	@RequestMapping(value="query")
	@ResponseBody
	public Map<String, Object> query( @RequestParam String userId) {
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String,Object> dataValue = new HashMap<String,Object>();
		Pager<UserMessage> pager = new Pager<UserMessage>();
		pager.setReload(true);
		try {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("userId", userId);
		params.put("orderBy", "create_time");
		params.put("order", "desc");		
		List<UserMessage> messages=new ArrayList<UserMessage>();
	
			 messages= this.getBaseService().findList(params);

			 //Map<String,Object> freMes= new HashMap<String,Object>();//好友0
			 Map<String,Object> jobMes= new HashMap<String,Object>();//职位1
			 Map<String,Object> sysMes= new HashMap<String,Object>();//系统2
			 Map<String,Object> serMes= new HashMap<String,Object>();//客服 3
			/* freMes.put("message", null);
			 freMes.put("messageType", 0);
			 freMes.put("finalTime",null);*/
			 jobMes.put("message", null);
			 jobMes.put("messageType", 1);
			 jobMes.put("finalTime",null);
			 sysMes.put("message", null);
			 sysMes.put("messageType", 2);
			 sysMes.put("finalTime",null);   
			 serMes.put("message", null);
			 serMes.put("messageType", 3);
			 serMes.put("finalTime",null);
			 int count0=0;
			 int count1=0;
			 int count2=0;
			 int count3=0;
        for (UserMessage message : messages) {     
        		/*if(freMes.get("message")==null&&message.getType()==0){
        			 freMes.put("message", message.getContent());                	
                	 freMes.put("finalTime",message.getCreateTime());
        		}   */	       	
	        	if(jobMes.get("message")==null&&message.getType()==1){
	        		
	        		 jobMes.put("message", message.getContent());		           	
		           	 jobMes.put("finalTime",message.getCreateTime());
	        	}
				if(sysMes.get("message")==null&&message.getType()==2){					        		
					 sysMes.put("message", message.getContent());		        	
		        	 sysMes.put("finalTime",message.getCreateTime());   		
			   }
				if(serMes.get("message")==null&&message.getType()==3){					
					 serMes.put("message", message.getContent());		       	
			         serMes.put("finalTime",message.getCreateTime());
				}
				if(message.getType()==0&&message.getIsRead()==0){
					count0++;
				}else if(message.getType()==1&&message.getIsRead()==0){
					count1++;
				}else if(message.getType()==2&&message.getIsRead()==0){
					count2++;
				}else if(message.getType()==3&&message.getIsRead()==0){
					count3++;
				}
		}             
        	
        	 //freMes.put("notReadCount", count0);
        	 jobMes.put("notReadCount", count1);
        	 sysMes.put("notReadCount", count2);
       	     serMes.put("notReadCount", count3);  
       	 // dataValue.put("freMes", freMes);
       	  dataValue.put("jobMes", jobMes);
       	  dataValue.put("sysMes", sysMes);
       	  dataValue.put("serMes", serMes);
		
			data.put("result", dataValue);
			data.put("message","消息查询成功！");
			data.put("status", 1);
		} catch (Exception e) {
			// TODO: handle exception
			data.put("message","消息查询失败！");
			data.put("status", 0);
		}
		return data;
	}
	//-----相应消息类型的内容-----
	@RequestMapping(value="queryByType")
	@ResponseBody
	public Map<String, Object> queryByType(@RequestParam String pageno, @RequestParam String userId,@RequestParam String type) {
		Map<String, Object> data = new HashMap<String, Object>();
		Pager<UserMessage> pager = new Pager<UserMessage>();
		pager.setReload(true);
		Map<String,Object> params = new HashMap<String,Object>();
		try {
			if(Integer.parseInt(type)!=1){
				params.put("userId", userId);
				params.put("type", type);
				params.put("orderBy", "create_time");
				params.put("order", "desc");		
				Pager<UserMessage> p = this.getBaseService().findByPager(pager, params);
				List <UserMessage> jobMessages=p.getData();
				data.put("result", jobMessages);
			}else{
				List <Map<String,Object>> jobMessages=userMessageService.getJobMes(type, userId, pageno);
				data.put("result", jobMessages);
			}
			data.put("message","消息查询成功！");
			data.put("status", 1);
		} catch (Exception e) {
			// TODO: handle exception
			data.put("message","消息查询失败！");
			data.put("status", 0);
		}
		return data;
	}
    //-----将消息标记为已读-----
	@RequestMapping(value="read")
	@ResponseBody
	public Map<String, Object> read(@RequestParam int messageId) {
		Map<String, Object> data = new HashMap<String, Object>();
		UserMessage um= new UserMessage();
		um.setMessageId(messageId);
		um.setIsRead(1);//默认为0  点击阅读后为1
		UserMessage u =  this.getBaseService().update(um);
		if (u == null) {
			data.put("message", "阅读失败！");
			data.put("status", 0);
		} else {
			data.put("message", "阅读成功！");
			data.put("status", 1);
		}
		return data;
	}
	
	//-----删除消息-----
	@RequestMapping(value="delete")
	@ResponseBody
	public Map<String, Object> delete(@RequestParam String messageIds) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("messageIds", messageIds);
		int count = this.getBaseService().delete(params);
		Map<String, Object> data = new HashMap<String, Object>();
		if (count == 0) {
			data.put("message", "删除失败！");
			data.put("status", 0);
		} else {
			data.put("message", "删除成功！");
			data.put("status", 1);
		}
		return data;
	}

}
